package com.dyrj.ysk.util;

import com.alibaba.fastjson.JSON;
import org.apache.http.HttpResponse;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

/**
 * Http请求工具类
 * @author 吴侠志  2020/1/7
 * @version 1.0
 */
public class HttpUtil {

    public static String post(String url,String jsonParam) throws Exception {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        URIBuilder uriBuilder = new URIBuilder(url);
        //创建一个http post请求
        HttpPost httpPost = new HttpPost(uriBuilder.build());
        //设置请求头header
        httpPost.setHeader("Content-Type","application/json");
        //设置请求数据
        httpPost.setEntity(new StringEntity(jsonParam,"utf-8"));
        HttpResponse response = httpClient.execute(httpPost);
        String content = EntityUtils.toString(response.getEntity());
        return content;
    }

}
